//-----------------------------------------------------------------------
// <copyright file="a_DesignerDAOBase.cs" company="AACECOM">
//     Copyright (c) AACECOM. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

using System;
using System.Data;
using System.Data.Common;
using System.Collections;
using Microsoft.Practices.EnterpriseLibrary.Data;
using ThoiTrang.Entities;

namespace ThoiTrang.Data
{    
    public abstract class a_DesignerDAOBase
    {
        #region Common methods
        public virtual a_Designer Createa_DesignerFromReader(IDataReader reader)
        {
            a_Designer item = new a_Designer();
            try
            {
                if (!reader.IsDBNull(reader.GetOrdinal("DID"))) item.DID = (int)reader["DID"];
                if (!reader.IsDBNull(reader.GetOrdinal("DesignerNameVN"))) item.DesignerNameVN = (string)reader["DesignerNameVN"];
                if (!reader.IsDBNull(reader.GetOrdinal("DesignerNameEN"))) item.DesignerNameEN = (string)reader["DesignerNameEN"];
            }
            catch (Exception ex)
            {                
                throw ex;
            }
            return item;
        }
        #endregion
        
        #region Createa_Designer methods
            
        public virtual void Createa_Designer(a_Designer a_Designer)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_Designers_ins");
                
                database.AddInParameter(dbCommand, "@DesignerNameVN", DbType.String, a_Designer.DesignerNameVN);
                database.AddInParameter(dbCommand, "@DesignerNameEN", DbType.AnsiString, a_Designer.DesignerNameEN);
                database.AddOutParameter(dbCommand, "@DID", DbType.Int32, 0);
                
                database.ExecuteNonQuery(dbCommand);
                a_Designer.DID = (int)database.GetParameterValue(dbCommand, "@DID");
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        #endregion

        #region Updatea_Designer methods
        
        public virtual void Updatea_Designer(a_Designer a_Designer)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_Designers_upd");            
                
                database.AddInParameter(dbCommand, "@DID", DbType.Int32, a_Designer.DID);
                database.AddInParameter(dbCommand, "@DesignerNameVN", DbType.String, a_Designer.DesignerNameVN);
                database.AddInParameter(dbCommand, "@DesignerNameEN", DbType.AnsiString, a_Designer.DesignerNameEN);
                
                database.ExecuteNonQuery(dbCommand);
            }
            catch (Exception ex)
            {
               throw ex;
            }
        }
        
        #endregion

        #region Deletea_Designer methods
        public virtual void Deletea_Designer(int did)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_Designers_del");
                
                database.AddInParameter(dbCommand, "@DID", DbType.Int32, did);
                
                database.ExecuteNonQuery(dbCommand);
            }
            catch (Exception ex)
            {
               throw ex;
            }
        }

        #endregion

        #region Geta_Designer methods
        
        public virtual a_Designer Geta_Designer(int did)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_Designers_sel");
                
                database.AddInParameter(dbCommand, "@DID", DbType.Int32, did);
                
                a_Designer a_Designer =  null;
                using (IDataReader reader = database.ExecuteReader(dbCommand))
                {
                    if (reader.Read())
                    {
                        a_Designer = Createa_DesignerFromReader(reader);
                        reader.Close();
                    }
                }
                return a_Designer;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        
        #endregion

        #region Geta_DesignerList methods
        public virtual a_DesignerCollection Geta_DesignerList(a_DesignerColumns orderBy, OrderDirection orderDirection, int page, int pageSize, out int totalRecords)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_Designers_selList");
                
                database.AddInParameter(dbCommand, "@OrderBy", DbType.AnsiString, orderBy.ToString());
                database.AddInParameter(dbCommand, "@OrderDirection", DbType.AnsiString, orderDirection.ToString());
                database.AddInParameter(dbCommand, "@Page", DbType.Int32, page);
                database.AddInParameter(dbCommand, "@PageSize", DbType.Int32, pageSize);
                database.AddOutParameter(dbCommand, "@TotalRecords", DbType.Int32, 4);
                
                a_DesignerCollection a_DesignerCollection = new a_DesignerCollection();
                using (IDataReader reader = database.ExecuteReader(dbCommand))
                {
                    while (reader.Read())
                    {
                        a_Designer a_Designer = Createa_DesignerFromReader(reader);
                        a_DesignerCollection.Add(a_Designer);
                    }
                    reader.Close();
                }
                totalRecords = (int)database.GetParameterValue(dbCommand, "@TotalRecords");
                return a_DesignerCollection;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        
        public virtual a_DesignerCollection Geta_DesignerList(a_DesignerColumns orderBy, OrderDirection orderDirection)
        {            
            int totalRecords = 0;
            return Geta_DesignerList(orderBy, orderDirection, 0, 0, out totalRecords);
        }
        
        #endregion
    }
}